package com.internal.Internalhibernate.server.dao;

// Generated Feb 24, 2012 1:32:12 PM by Hibernate Tools 3.3.0.GA

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.internal.Internalhibernate.client.AdmProfessions;
import com.internal.Internalhibernate.server.SessionFactoryUtil;

/**
 * Home object for domain model class AdmProfessions.
 * @see com.internal.Internalhibernate.client.AdmProfessions
 * @author Hibernate Tools
 */
public class AdmProfessionsDAO {

	private static final Log log = LogFactory.getLog(AdmProfessionsDAO.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil
			.getSessionFactory();

	public void persist(AdmProfessions transientInstance) {
		log.debug("persisting AdmProfessions instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(AdmProfessions instance) {
		log.debug("attaching dirty AdmProfessions instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AdmProfessions instance) {
		log.debug("attaching clean AdmProfessions instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(AdmProfessions persistentInstance) {
		log.debug("deleting AdmProfessions instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public AdmProfessions merge(AdmProfessions detachedInstance) {
		log.debug("merging AdmProfessions instance");
		try {
			AdmProfessions result = (AdmProfessions) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public AdmProfessions findById(short id) {
		log.debug("getting AdmProfessions instance with id: " + id);
		try {
			AdmProfessions instance = (AdmProfessions) sessionFactory
					.getCurrentSession().get(
							"com.internal.Internalhibernate.client.AdmProfessions", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findAll() {
		Session session = sessionFactory.getCurrentSession();
	    session.beginTransaction();
		Query q = session.createQuery("from AdmProfessions order by PRO_ID");
		return q.list();
	}
	
	public List findProByName(String name) {
		Session session = sessionFactory.getCurrentSession();
	    session.beginTransaction();
		Query q = session.createQuery("from AdmProfessions where profession like '%"+name+"%' and rownum < 10");
		return q.list();
	}
	
	public String getProfName(short id){
		try {
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			Query q = session.createQuery("select profession from AdmProfessions where proId = '"+id+"'");	
			return (String)q.uniqueResult();
		}catch (RuntimeException re) {
			log.error("get prof name by id failed :", re);
			throw re;
		}
	}
}
